import sys
from pathlib import Path

# 将项目根目录添加到 Python 路径
sys.path.append(str(Path(__file__).parent))

import streamlit as st
from ui.pages.home import home_page as show_home
from ui.pages.scene_management import show as show_scene_management
from ui.pages.parameter_setup import show_parameter_setup
from ui.pages.data_import import show_data_import
from ui.pages.data_preparation import show_data_preparation
from ui.pages.calculation import show_calculation
from ui.pages.results import show_results
from ui.components.sidebar import render_sidebar
from ui.state_manager import initialize_session_state

def main():
    """
    主应用入口点，初始化Streamlit应用并管理页面导航
    """
    PAGE_CONFIG = {
        "page_title": "智能测算系统",
        "page_icon": "📊",
        "layout": "wide",
        "initial_sidebar_state": "expanded",
    }
    st.set_page_config(**PAGE_CONFIG)
    
    # 初始化会话状态
    try:
        initialize_session_state()
    except Exception as e:
        st.error(f"初始化会话状态失败: {e}")
        st.session_state.current_page = "home"  # 默认跳转到首页
    
    # 渲染侧边栏导航
    render_sidebar()
    
    # 根据当前页面状态显示相应页面
    page_handlers = {
        "home": show_home,
        "scene_management": show_scene_management,
        "parameter_setup": show_parameter_setup,
        "data_import": show_data_import,
        "data_preparation": show_data_preparation,
        "calculation": show_calculation,
        "results": show_results,
    }
    page_handlers.get(st.session_state.current_page, show_home)()

if __name__ == "__main__":
    main()